#include "h264_live_video_server_media_subssion.hpp"
#include "H264VideoStreamFramer.hh"
#include "H264VideoRTPSink.hh"
#include "H264VideoStreamDiscreteFramer.hh"
#include  "h264_framed_live_source.hpp"
H264LiveVideoServerMediaSubssion *
H264LiveVideoServerMediaSubssion::createNew(UsageEnvironment &env, Boolean reuse_first_source)
{
    return new H264LiveVideoServerMediaSubssion(env, reuse_first_source);
}

H264LiveVideoServerMediaSubssion::H264LiveVideoServerMediaSubssion(UsageEnvironment &env, Boolean reuse_first_source) : OnDemandServerMediaSubsession(env, reuse_first_source)
{
    device_ = new VideoDevice();
}

H264LiveVideoServerMediaSubssion::~H264LiveVideoServerMediaSubssion()
{
    LOG(INFO)<<"delete device";
    delete device_;
}

FramedSource *H264LiveVideoServerMediaSubssion::createNewStreamSource(unsigned client_session_id, unsigned &est_bitrate)
{
    //此处创建视频源， 参照H264VideoFileServerMediaSubsession
    //注意使用H264VideoStreamDiscreteFramer去除H264的分片操作，但是输入的H264数据必须去除NAL头
    H264FramedLiveSource* lived_source = H264FramedLiveSource::createNew(envir(),device_);
    if(NULL == lived_source)
    {
        return NULL;
    }
    LOG(INFO)<<"create H264FramedLiveSource finishe";
    //return H264VideoStreamDiscreteFramer::createNew(envir(), lived_source);
    return H264VideoStreamFramer::createNew(envir(),lived_source);
}

RTPSink *H264LiveVideoServerMediaSubssion::createNewRTPSink(Groupsock *rtp_groupsock,
                                                            unsigned char rtp_payload_type_if_dynamic,
                                                            FramedSource *input_source)
{
    input_source=NULL;
    return H264VideoRTPSink::createNew(envir(), rtp_groupsock, rtp_payload_type_if_dynamic);
}